package com.hs.telepass.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnItemClick;
import butterknife.OnItemSelected;
import com.hs.teleform.R;
import com.hs.telepass.manager.ProfilesManager;
import com.hs.telepass.manager.SingouManager;
import com.hs.telepass.model.Profile;
import com.hs.telepass.dao.TelepassProfilesDatabase;
import com.hs.telepass.model.SingouProfiles;
import com.hs.telepass.ui.Adapter.ProfileAdapter;
import com.hs.telepass.utils.ToastUtil;

import java.util.List;

public class HistoryFragment extends Fragment implements LoaderManager.LoaderCallbacks {
    @InjectView(R.id.listview)
    ListView mListview;

    @OnItemClick(R.id.listview)
    void onItemSelected(int position) {
        Profile profile = mProfilesList.get(position);
        Intent intent = new Intent(getActivity(), EditUserInfoActivity.class);
        intent.putExtra(EXTRA_PROFILE, profile);
        startActivity(intent);
    }
    public static final String EXTRA_PROFILE = "extra_profile";

    private Context mContext;
    private ProfileAdapter profileAdapter;
    private View mRootView;
    private List<Profile> mProfilesList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflateAndSetupView(inflater, container, savedInstanceState, R.layout.history_fragment);
    }


    // 生成并设置根视图
    private View inflateAndSetupView(LayoutInflater inflater,
                                     ViewGroup container, Bundle savedInstanceState, int layoutResourceId) {
        mRootView = inflater.inflate(layoutResourceId, container, false);
        ButterKnife.inject(this, mRootView);
        return mRootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onResume() {
        super.onResume();
        initTelepassProfilesList();
    }


    private void initTelepassProfilesList() {
        mProfilesList = ProfilesManager.getInstance().getNewProfiles();
        profileAdapter = new ProfileAdapter(mContext, mProfilesList);
        mListview.setAdapter(profileAdapter);

    }

    @Override
    public Loader onCreateLoader(int i, Bundle bundle) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader loader, Object o) {

    }

    @Override
    public void onLoaderReset(Loader loader) {

    }
}